package movieGandalf.DB;

import java.security.MessageDigest;

public class Encoder {
	private MessageDigest mg;
	
	public Encoder(){
		try{
			mg = MessageDigest.getInstance("MD5");
		}
		catch(Exception e){
			mg = null;
		}
	}
	
	public Long encode(String in){
		if (mg==null)
			return null;
		
		byte[] inBytes = in.getBytes();
		byte[] digest = mg.digest(inBytes);
		
		return Encoder.bytesToLong(digest);
	}
	
	public static Long bytesToLong(byte[] bytes){
		Long value = new Long(0);
		for (int i = 0; i < bytes.length; i++){
		   value = (value << 8) + (bytes[i] & 0xff);
		}
		
		return value;
	}
}
